#include <am.h>
#include <klib.h>
#include <klib-macros.h>

#define IP_BASE_ADDR 0x10004000
#define REG_CTRL *((volatile uint32_t *)(IP_BASE_ADDR + 0))
#define REG_PSCR *((volatile uint32_t *)(IP_BASE_ADDR + 4))
#define REG_CNT  *((volatile uint32_t *)(IP_BASE_ADDR + 8))
#define REG_CMP  *((volatile uint32_t *)(IP_BASE_ADDR + 12))
#define REG_STAT *((volatile uint32_t *)(IP_BASE_ADDR + 16))
#define REG_KEY  *((volatile uint32_t *)(IP_BASE_ADDR + 20))
#define REG_FEED *((volatile uint32_t *)(IP_BASE_ADDR + 24))

#define WDG_MAGIC_NUM (uint32_t)0x5F3759DF

int main(){
    putstr("wdg test\n");
    
    REG_KEY = WDG_MAGIC_NUM;
    REG_CTRL = (uint32_t) 0x0;

    REG_KEY = WDG_MAGIC_NUM;
    REG_PSCR = (uint32_t) 0x10;

    REG_KEY = WDG_MAGIC_NUM;
    REG_CMP = (uint32_t) 0xF;

    while(REG_STAT == (uint32_t)0x1); // clear irq flag

    REG_KEY = WDG_MAGIC_NUM;
    REG_CTRL = (uint32_t) 0x5;

    REG_KEY = WDG_MAGIC_NUM;
    REG_FEED = (uint32_t) 0x1;

    REG_KEY = WDG_MAGIC_NUM;
    REG_FEED = (uint32_t) 0x0;

    for(int i = 0; i < 10; ++i) {
        while(REG_STAT == 0);
        printf("%d wdg reset trigger\n", i);
    }
    return 0;
}
